package com.budgetme.activities.elements;

import com.budgetme.ui.fragments.BudgetDetailsFragment;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;


public class BudgetProgressBar extends ImageView {
	private static final String TAG = BudgetProgressBar.class.getName();

	private static final int BASE_RECT_BORDER_WIDTH = 4;

	private Paint mPaint;
	private Context mContext;
	
	private RectF rectf = new RectF();
	private Rect rect = new Rect();
	
	private int mBaseColor;
	private int mProgressColor;
	private int mDiffColor;
	
	private int mViewHeight;
	private int mViewWidth;
	
	private int mBarPadding = 3;
	
	private Rect mBaseBar = new Rect();
	private Rect mProgressBar = new Rect();
	private Rect mDiffBar = new Rect();
	
	private double mMaxProgress = 0;
	private double mBaselineProgress = 0;
	private double mActualProgress = 0;
	
	private int mMaxProgressNormalized;
	private int mBaselineProgressNormalized;
	private int mActualProgressNormalized;
	
	private ShapeDrawable mBaseBarShape;
	private ShapeDrawable mLeftBarShape;
	private ShapeDrawable mStraightBarShape;
	private ShapeDrawable mDiffBarShape;
	
	
	public BudgetProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mPaint = new Paint();
		mContext = context;
		
		// TODO: God with red base when over budget.  diff color green/red for ahead/behind.  Progress?
		mBaseColor = mContext.getResources().getColor(android.R.color.holo_blue_dark);
		mProgressColor = mContext.getResources().getColor(android.R.color.holo_blue_dark);
		mDiffColor = mContext.getResources().getColor(android.R.color.holo_green_light);
		
		mLeftBarShape = new ShapeDrawable(new RoundRectShape(new float[]{ 3, 3, 0, 0, 0, 0, 3, 3 }, null, null));
		mStraightBarShape = new ShapeDrawable(new RoundRectShape(new float[]{ 0, 0, 0, 0, 0, 0, 0, 0 }, null, null));
		mBaseBarShape = new ShapeDrawable(
				new RoundRectShape(
						new float[]{ 6, 6, 6, 6, 6, 6, 6, 6 }, 
						new RectF(BASE_RECT_BORDER_WIDTH, BASE_RECT_BORDER_WIDTH, BASE_RECT_BORDER_WIDTH, BASE_RECT_BORDER_WIDTH), 
						new float[]{ 3, 3, 3, 3, 3, 3, 3, 3 }));
		
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		//mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
		
		mBaseBarShape.setBounds(mBaseBar);
		mBaseBarShape.getPaint().setColor(mBaseColor);
		mBaseBarShape.draw(canvas);

//		rect.set(mProgressBar);
		mLeftBarShape.setBounds(mProgressBar);
		mLeftBarShape.getPaint().setColor(mProgressColor);
		mLeftBarShape.draw(canvas);
		
		
		if (mActualProgressNormalized == 0) {
			mLeftBarShape.setBounds(mDiffBar);
			mLeftBarShape.getPaint().setColor(mDiffColor);
			mLeftBarShape.draw(canvas);
		} else {
			mStraightBarShape.setBounds(mDiffBar);
			mStraightBarShape.getPaint().setColor(mDiffColor);
			mStraightBarShape.draw(canvas);
		}

	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	    mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
	    mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
	    
	    mBaseBar.set(mBarPadding, mBarPadding, mViewWidth - mBarPadding, mViewHeight - mBarPadding);
	    
	    mMaxProgressNormalized = mBaseBar.width();
	    
	    normalize();
	    
	}
	
	private void normalize() {
		
		mBaselineProgressNormalized = getNormalizedProgress(mBaselineProgress);
		mActualProgressNormalized = getNormalizedProgress(mActualProgress);
		
		mProgressBar.set(mBarPadding + BASE_RECT_BORDER_WIDTH, mBarPadding + BASE_RECT_BORDER_WIDTH, mActualProgressNormalized, mViewHeight - mBarPadding - BASE_RECT_BORDER_WIDTH);
		
		if (mActualProgressNormalized == 0) {
			mDiffBar.set(mBarPadding + BASE_RECT_BORDER_WIDTH, mBarPadding + BASE_RECT_BORDER_WIDTH, mBaselineProgressNormalized, mViewHeight - mBarPadding - BASE_RECT_BORDER_WIDTH);
			mDiffBarShape = mLeftBarShape;
		} else if (mBaselineProgressNormalized > mActualProgressNormalized) {
			mDiffBar.set(mActualProgressNormalized, mBarPadding + BASE_RECT_BORDER_WIDTH, mBaselineProgressNormalized, mViewHeight - mBarPadding - BASE_RECT_BORDER_WIDTH);
			mDiffBarShape = mLeftBarShape;
		} else {
			mDiffBar.set(mBaselineProgressNormalized, mBarPadding + BASE_RECT_BORDER_WIDTH, mActualProgressNormalized, mViewHeight - mBarPadding - BASE_RECT_BORDER_WIDTH);
		}

		invalidate();
		
	}
	
	private int getNormalizedProgress(double value) {	
		return (int) (mMaxProgressNormalized * value / mMaxProgress);
	}
	
	public void setProgress(double baseline, double actual) {
		
		mBaselineProgress = baseline;
		mActualProgress = actual;
		
		normalize();
		
	}
	
	public void setMaxProgress(double max) {
		
		mMaxProgress = max;
		
		normalize();
		
	}
	
	

	
	

}
